import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent, STColumnBadge, STData, STChange } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { SysUserEditComponent } from './edit/edit.component';
import { UserService, DictService } from '@core';
import { environment } from '@env/environment';

@Component({
  selector: 'app-sys-user',
  templateUrl: './user.component.html',
})
export class SysUserComponent implements OnInit {
  isGeneralQuery = true;
  selectedRows: STData[] = [];
  keyword: string = '';
  
  sexBadge: STColumnBadge = {};

  constructor(
    private http: _HttpClient,
    private msgSrv: NzMessageService,
    private modal: ModalHelper,
    private userService: UserService,
    private dictService: DictService,
  ) {
    this.sexBadge = { X: { text: '未知', color: 'success' } };
  }

  url = `${environment.sys.ctx}/sysUser/findByQueryParams`;

  sexs = { X: '未知' };

  searchSchema: SFSchema = {
    properties: {
      username: {
        type: 'string',
        title: '用户名',
      },
      petname: {
        type: 'string',
        title: '昵称',
      },
      realname: {
        type: 'string',
        title: '真实姓名',
      },
      genderArray: {
        type: 'string',
        title: '性别',
        ui: {
          widget: 'checkbox',
          asyncData: () => this.dictService.getSFSchemaEnumTypes('GENDER'),
        },
      },
    },
  };

  @ViewChild('st', { static: true }) st!: STComponent;
  columns: STColumn[] = [
    { title: 'ID', index: 'id', type: 'checkbox' },
    { title: '用户名', index: 'username' },
    { title: '昵称', index: 'petname' },
    { title: '真实姓名', index: 'realname' },
    { title: '性别', index: 'genderName', render: 'columnSex' },
    { title: '生日', type: 'date', index: 'birthday' },
    { title: '电话', index: 'phone' },
    { title: '邮箱', index: 'email' },
    // { title: '头像', type: 'img', width: '50px', index: 'avatar' },
    { title: '创建时间', type: 'date', index: 'createdDate' },
    {
      title: '创建人',
      index: 'createdByName',
    },
    {
      title: '操作',
      width: 100,
      buttons: [
        // { text: '查看', click: (item: any) => `/form/${item.id}` },
        { icon: 'edit', type: 'static',  modal: {component: SysUserEditComponent }, click: 'reload', tooltip: `编辑` },
        {
          tooltip: `删除`,
          icon: 'delete',
          type: 'del',
          pop: {
            title: '是否确定删除?',
            okType: 'danger',
            icon: 'star',
          },
          click: (record, _modal, comp) => this.delete(record, _modal, comp),
        },
      ],
    },
  ];

  ngOnInit() {}

  delete(record: any, _modal: any, comp: any) {
    this.http.post(`${environment.sys.ctx}/sysUser/deleteById/${record.id}`).subscribe(res => {
      this.msgSrv.success(`成功删除`);
      this.st.reload();
    });
  }

  add() {
    this.modal.createStatic(SysUserEditComponent, { i: {} }).subscribe(() => this.st.reload());
  }

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  
  deletes() {
    const ids = this.selectedRows.map((row)=>{return row.id})
    this.http.post(`${environment.sys.ctx}/sysUser/deleteByIds`,ids).subscribe(res => {
      this.msgSrv.success(`成功删除`);
      this.st.reload();
    });
  }
}
